package fr.irit.etracetool.simpleemf;

import fr.irit.etracetool.etrace.*;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.uml2.uml.*;

public class EtraceToDot
{
	  protected static String nl;
	  public static synchronized EtraceToDot create(String lineSeparator)
	  {
	    nl = lineSeparator;
	    EtraceToDot result = new EtraceToDot();
	    nl = null;
	    return result;
	  }

	  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
	  protected final String TEXT_1 = "\t";
	  protected final String TEXT_2 = NL;
	  protected final String TEXT_3 = NL + "\t\t\t";
	  protected final String TEXT_4 = "[label=\"";
	  protected final String TEXT_5 = "\",style=filled, color=orange];" + NL + "\t\t\t";
	  protected final String TEXT_6 = "[label=\"";
	  protected final String TEXT_7 = "\"];" + NL + "\t\t\t";
	  protected final String TEXT_8 = "->";
	  protected final String TEXT_9 = "[label=\"";
	  protected final String TEXT_10 = "\", color=orange];" + NL + "\t\t";
	  protected final String TEXT_11 = NL + "\t\t\tsubgraph cluster_";
	  protected final String TEXT_12 = " {" + NL + "\t\t\t\tlabel = \"";
	  protected final String TEXT_13 = "\";" + NL + "\t\t\t\t";
	  protected final String TEXT_14 = NL + "\t\t\t}" + NL + "\t\t";
	  protected final String TEXT_15 = NL + "\t\t";
	  protected final String TEXT_16 = "[label=\"";
	  protected final String TEXT_17 = "\", shape=house, style=filled];" + NL + "\t\t";
	  protected final String TEXT_18 = "[label=\"";
	  protected final String TEXT_19 = "\", style=filled, color=yellow];" + NL + "\t\t";
	  protected final String TEXT_20 = "->";
	  protected final String TEXT_21 = "[label=\"";
	  protected final String TEXT_22 = "\"];" + NL + "\t";
	  protected final String TEXT_23 = NL + "\t\t";
	  protected final String TEXT_24 = "[label=\"";
	  protected final String TEXT_25 = "\"];" + NL + "\t\t";
	  protected final String TEXT_26 = "[label=\"";
	  protected final String TEXT_27 = "\"];" + NL + "\t\t";
	  protected final String TEXT_28 = "->";
	  protected final String TEXT_29 = "[label=\"";
	  protected final String TEXT_30 = "\", style=dotted];" + NL + "\t";
	  protected final String TEXT_31 = NL + "\t\t";
	  protected final String TEXT_32 = "[label=\"";
	  protected final String TEXT_33 = "\"];" + NL + "\t\t";
	  protected final String TEXT_34 = "[label=\"";
	  protected final String TEXT_35 = "\"];" + NL + "\t\t";
	  protected final String TEXT_36 = "->";
	  protected final String TEXT_37 = "[label=\"";
	  protected final String TEXT_38 = "\", color=green];" + NL + "\t";
	  protected final String TEXT_39 = NL + "\t\t";
	  protected final String TEXT_40 = "[label=\"";
	  protected final String TEXT_41 = "\"];" + NL + "\t\t";
	  protected final String TEXT_42 = "[label=TRASH, shape=box, style=filled];" + NL + "\t\t";
	  protected final String TEXT_43 = "->";
	  protected final String TEXT_44 = "[color=red];" + NL + "\t";
	  protected final String TEXT_45 = NL + NL + "digraph G {" + NL + "\tlabel = \"";
	  protected final String TEXT_46 = "\";" + NL + "\t";
	  protected final String TEXT_47 = NL + "}";
	  protected final String TEXT_48 = NL;

	  @Deprecated
	  public String generate(Object argument)
	  {
	    final StringBuffer stringBuffer = new StringBuffer();
	    stringBuffer.append(TEXT_1);
	    ETrace trace = (ETrace) argument; 
	    stringBuffer.append(TEXT_2);
	    
	class Util {
		int id=1;
		public void firstCategory(AbstractLink l) {
			id++;
			if (l instanceof Link) {
	    stringBuffer.append(TEXT_3);
	    stringBuffer.append(l.getSource().hashCode());
	    stringBuffer.append(TEXT_4);
	    stringBuffer.append(((NamedElement) l.getSource()).getName());
	    stringBuffer.append(TEXT_5);
	    stringBuffer.append(l.getTarget().hashCode());
	    stringBuffer.append(TEXT_6);
	    stringBuffer.append(((NamedElement) l.getTarget()).getName());
	    stringBuffer.append(TEXT_7);
	    stringBuffer.append(l.getSource().hashCode());
	    stringBuffer.append(TEXT_8);
	    stringBuffer.append(l.getTarget().hashCode());
	    stringBuffer.append(TEXT_9);
	    stringBuffer.append(l.getType().getName());
	    stringBuffer.append(TEXT_10);
	    }
			if (l instanceof CompositeLink) {
	    stringBuffer.append(TEXT_11);
	    stringBuffer.append(l.hashCode());
	    stringBuffer.append(TEXT_12);
	    stringBuffer.append(l.getType().getName());
	    stringBuffer.append(TEXT_13);
	    EList<AbstractLink> children = ((CompositeLink)l).getChildren();
					Util u=new Util();
					for (int i=0; i<children.size(); i++) {
						if(children.get(i).getType().getSuperType().getName() == "Fonctionnal Method") {
							u.firstCategory(children.get(i));
						}
						if(children.get(i).getType().getSuperType().getName() == "Factory method") {
							u.secondCategory(children.get(i),id);
						}
						if(children.get(i).getType().getSuperType().getName() == "Setter Method") {
							u.thirdCategory(children.get(i),id);
						}
						if(children.get(i).getType().getSuperType().getName() == "Add Feature") {
							u.fourthCategory(children.get(i));
						}
						if(children.get(i).getType().getSuperType().getName() == "Remove Feature") {
							u.fifthCategory(children.get(i), id);
						}
					}
					
	    stringBuffer.append(TEXT_14);
	    }
		}
		
		public void secondCategory(AbstractLink l, int id) {
	    stringBuffer.append(TEXT_15);
	    stringBuffer.append(l.getSource().hashCode()+id);
	    stringBuffer.append(TEXT_16);
	    stringBuffer.append(l.getSource().eClass().getName());
	    stringBuffer.append(TEXT_17);
	    stringBuffer.append(l.getTarget().hashCode());
	    stringBuffer.append(TEXT_18);
	    stringBuffer.append(((NamedElement) l.getTarget()).getName());
	    stringBuffer.append(TEXT_19);
	    stringBuffer.append(l.getSource().hashCode()+id);
	    stringBuffer.append(TEXT_20);
	    stringBuffer.append(l.getTarget().hashCode());
	    stringBuffer.append(TEXT_21);
	    stringBuffer.append(l.getType().getName());
	    stringBuffer.append(TEXT_22);
	    }
		public void thirdCategory(AbstractLink l,int id) {
	    stringBuffer.append(TEXT_23);
	    stringBuffer.append(l.getSource().hashCode()+id);
	    stringBuffer.append(TEXT_24);
	    stringBuffer.append(((NamedElement) l.getSource()).getName());
	    stringBuffer.append(TEXT_25);
	    stringBuffer.append(l.getTarget().hashCode());
	    stringBuffer.append(TEXT_26);
	    stringBuffer.append(((NamedElement) l.getTarget()).getName());
	    stringBuffer.append(TEXT_27);
	    stringBuffer.append(l.getSource().hashCode()+id);
	    stringBuffer.append(TEXT_28);
	    stringBuffer.append(l.getTarget().hashCode());
	    stringBuffer.append(TEXT_29);
	    stringBuffer.append(l.getType().getName());
	    stringBuffer.append(TEXT_30);
	    
		}
		public void fourthCategory(AbstractLink l) {
		if (l.getTarget() != null) {
	    stringBuffer.append(TEXT_31);
	    stringBuffer.append(l.getSource().hashCode());
	    stringBuffer.append(TEXT_32);
	    stringBuffer.append(((NamedElement) l.getSource()).getName());
	    stringBuffer.append(TEXT_33);
	    stringBuffer.append(l.getTarget().hashCode());
	    stringBuffer.append(TEXT_34);
	    stringBuffer.append(((NamedElement) l.getTarget()).getName());
	    stringBuffer.append(TEXT_35);
	    stringBuffer.append(l.getSource().hashCode());
	    stringBuffer.append(TEXT_36);
	    stringBuffer.append(l.getTarget().hashCode());
	    stringBuffer.append(TEXT_37);
	    stringBuffer.append(l.getType().getName());
	    stringBuffer.append(TEXT_38);
		}
		}
		public void fifthCategory(AbstractLink l, int id) {
	    stringBuffer.append(TEXT_39);
	    stringBuffer.append(l.getSource().hashCode());
	    stringBuffer.append(TEXT_40);
	    stringBuffer.append(((NamedElement) l.getSource()).getName());
	    stringBuffer.append(TEXT_41);
	    stringBuffer.append(id);
	    stringBuffer.append(TEXT_42);
	    stringBuffer.append(l.getSource().hashCode());
	    stringBuffer.append(TEXT_43);
	    stringBuffer.append(id);
	    stringBuffer.append(TEXT_44);
	    
		}
	}
		
	    stringBuffer.append(TEXT_45);
	    stringBuffer.append(trace.getName());
	    stringBuffer.append(TEXT_46);
	    
		EList<AbstractLink> children = trace.getChildren();
		Util u=new Util();
		for (int i=0; i<children.size(); i++) {
			if(children.get(i).getType().getSuperType().getName() == "Fonctionnal Method") {
				u.firstCategory(children.get(i));
			}
			if(children.get(i).getType().getSuperType().getName() == "Factory method") {
				u.secondCategory(children.get(i),u.id);
			}
			if(children.get(i).getType().getSuperType().getName() == "Setter Method") {
				u.thirdCategory(children.get(i),u.id);
			}
			if(children.get(i).getType().getSuperType().getName() == "Add Feature") {
				u.fourthCategory(children.get(i));
			}
			if(children.get(i).getType().getSuperType().getName() == "Remove Feature") {
				u.fifthCategory(children.get(i), u.id);
			}
		}
		
	    stringBuffer.append(TEXT_47);
	    stringBuffer.append(TEXT_48);
	    return stringBuffer.toString();
	  }
}
